<?php

/*
 * 核心类
 */
class Core{
    public function __construct() {
        
    }
}

/*
 * 控制器类*
 */
class Controller {
    public $_view;
    
    function __construct() {
        $this->_view = new View();
    }
}

/*
 *模型类
 */
class Model{
    public $_db;
    public $table;
    
    function __construct() {
        $this->_db = PDOMysql::getInstance('open_base');
    }
    
    public static function getSqlData($sql_data,$separator=' and '){
        $where = $new_sql_data = array();
        foreach ($sql_data as $key => $value) {
            //开始结束时间
            if($key == "start_time"){
                $where[] = "add_time>=:$key";
            }elseif ($key == "end_time") {
                $where[] = "add_time<=:$key";
            }else{
                $where[] = "$key=:$key";
            }
            $new_sql_data[":$key"] = $value;
        }
        $where = implode($separator, $where);
        return array($where,$new_sql_data);
    }
    
    // 搜索一条数据
    public function selectRow($table,$sql_data){
        list($where,$new_sql_data) = self::getSqlData($sql_data);
        $sql = "select * from $table where $where";
        $result = $this->_db->fetchRow($sql,$new_sql_data);
        return $result;
    }
    
    // 搜索count
    public function selectCount($table,$sql_data,$search='',$search_field=''){
        list($where,$new_sql_data) = self::getSqlData($sql_data);
        $sql = "select count(*) as ct from $table where $where";
        if($search && $search_field){
            $sql .= " and (";
            foreach ($search_field as $key => $value) {
                $search_sql[]= " $value like '%$search%' ";
            }
            $sql .= implode('or ', $search_sql);
            $sql .= ")";
        }
        $result = $this->_db->fetchRow($sql,$new_sql_data);
        return $result;
    }
    
    // 搜索批量数据
    public function selectAll($table,$sql_data,$page=1,$offset=20,$search='',$search_field='',$order="id DESC"){
        list($where,$new_sql_data) = self::getSqlData($sql_data);
        $sql = "select * from $table where $where";
        if($search && $search_field){
            $sql .= " and (";
            foreach ($search_field as $key => $value) {
                $search_sql[]= " $value like '%$search%' ";
            }
            $sql .= implode('or ', $search_sql);
            $sql .= ")";
        }
        $sql .= " order by $order";
//        echo $sql;die();
        if($page !=-1){
           $sql .= " limit ".($page-1)*$offset.",$offset"; 
        }
        $result = $this->_db->fetchAll($sql,$new_sql_data);
        return $result;
    }
    
    // 插入一条数据
    public function insertRow($table,$data,$is_return_id=TRUE){
        list($set,$sql_data) =  self::getSqlData($data,",");
        $sql = "insert into $table set $set";
        $res = $this->_db->excute($sql, $sql_data);
        if($is_return_id){
            return $this->_db->lastInsertId();
        }
        return $res;
    }
    
    // 更新数据
    public function updateRow($table,$data,$where){
        list($set,$sql_data) =  self::getSqlData($data,",");
        $sql = "update $table set  $set $where";
        $res = $this->_db->excute($sql, $sql_data);
        return $res;
    }
}

/*
 * 视图类
 */
class View {
    public $vars = array();
    
    public function display($view,$vars = array()){
        if(!empty($vars)){
            $this->vars = array_merge($this->vars,$vars);
        }
        if(is_array($this->vars)){
            extract($this->vars);
        }
        include template($view);
    }
}

class DT{
    public function run(){
        $module = Input::getString("m", "index-index");
        $method = Input::getString('do', "index");
        $module = explode("-", $module);
        //首字母大写
        foreach ($module as $key => $value) {
            $module[$key] = ucfirst($value);
        }
        //获取参数
        $params  = array();
        parse_str($_SERVER['QUERY_STRING'],$params);
        unset($params["m"]);
        unset($params["do"]);
        
       //解析module和method
        $file_path = __ROOT__.'controller/'.implode("/", $module).".class.php";
        if(file_exists($file_path)){
            require $file_path;
            $class = end($module);
            if(class_exists($class)){
                $App = new $class();
                if(method_exists($class, $method)){
                    $App->$method();
                }
            }
        }  else {
        }
    }
}

